package com.yxkj.syh.app.huarong.activities.orders.detail.status0;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bean.LiveDataEvent;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.config.BasicConstants;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.bean.Goods;
import com.yxkj.syh.app.huarong.bean.OrderDetailResponse;
import com.yxkj.syh.app.huarong.bean.OrderInfo;
import com.yxkj.syh.app.huarong.data_center.model.OrderModel;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建者：SYH
 * 时间：2020/5/29 13:08
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class OrderWaitVM extends BaseViewModel<ActivityEvent> {

    public OrderWaitVM(@NonNull Application application) {
        super(application);
    }


    public ObservableField<String> ofLineType = new ObservableField<>();
    public ObservableField<String> ofFromAddress = new ObservableField<>();
    public ObservableField<String> ofToAddress = new ObservableField<>();
    public ObservableField<String> ofCarHeadNo = new ObservableField<>();
    public ObservableField<String> ofCarTrailerNo = new ObservableField<>();
    public ObservableField<String> ofOrderNo = new ObservableField<>();
    public ObservableField<String> ofCreatTime = new ObservableField<>();

    public MutableLiveData<OrderInfo> mldOrderDetail = new MutableLiveData<>();

    public void getOrderDetail(long id) {
        OrderModel.getOrderModel().orderDetail(id, mLifecycleProvider.bindToLifecycle(), new ApiObserver<OrderDetailResponse>() {
            @Override
            public void onSuccess(OrderDetailResponse orderDetailResponse) {
                OrderInfo orderInfo = orderDetailResponse.getData();
                mldOrderDetail.setValue(orderDetailResponse.getData());

                ofLineType.set(orderInfo.getLineType() == 0 ? "直达" : "回头车");
                ofFromAddress.set(orderInfo.getStartArea()+orderInfo.getStartAddress());
                ofToAddress.set(orderInfo.getEndArea()+orderInfo.getEndAddress());
                ofCarHeadNo.set(orderInfo.getCarNo());
                ofCarTrailerNo.set(orderInfo.getSemitrailerNo());
                ofOrderNo.set(orderInfo.getNo());
                ofCreatTime.set(orderInfo.getCreateTime());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }

    public void cancelOrder() {
        Map<String, Object> pMap = new HashMap<>();
        pMap.put("orderId", mldOrderDetail.getValue().getId());

        showLoading("cancelOrder");
        OrderModel.getOrderModel().cancelOrder(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                hideLoading("cancelOrder");
                Tooast.success("已取消");
                EventBus.getDefault().post(new LiveDataEvent(BasicConstants.EVENT_SELECT_ORDER_ALL));
                finish();
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("cancelOrder");
                Tooast.warning(msg);
            }
        });
    }


}
